package controller

import (
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
	"gin-vue-admin/model/response"
	"gin-vue-admin/service"
	"gin-vue-admin/utils"
	"sort"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

// GetMenuList
// @Tags SysMenu
// @Summary 查询菜单列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.QueryMenu true " "
// @success 200 {object} response.Response{data=[]model.SysMenu} "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /menu/getMenuList [get]
func GetMenuList(c *gin.Context) {
	var menu request.QueryMenu
	_ = c.ShouldBindQuery(&menu)

	data, err := service.GetMenuInfoList(menu)
	list := data.GetRootTree()
	sort.Sort(list)
	list.SyntheticTree(data)

	response.RespondData(list, err, c)
}

// GetMenuList
// @Tags SysMenu
// @Summary 查询菜单列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.QueryMenu true " "
// @success 200 {object} response.Response{data=[]model.SysMenu} "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /menu/getMenuList [get]
func GetMenuTree(c *gin.Context) {
	data, err := service.GetMenuTree()
	list := data.GetRootTree()
	sort.Sort(list)
	list.SyntheticTree(data)

	response.RespondData(list, err, c)
}

// CreateMenu
// @Tags SysMenu
// @Summary 新增菜单
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SysMenu true " "
// @Success 200 {string} string "{"success":true,"data":{},"msg":"操作成功"}"
// @Router /menu/addMenu [post]
func CreateMenu(c *gin.Context) {
	var menu model.SysMenu
	_ = c.ShouldBindJSON(&menu)
	if err := utils.Verify(menu); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	claims, _ := c.Get("claims")
	user := claims.(*request.CustomClaims)
	menu.CreateTime = time.Now()
	menu.UpdateTime = time.Now()
	menu.CreateBy = user.Username
	menu.UpdateBy = user.Username

	err := service.CreateMenu(menu)
	response.RespondOperate(err, c)
}

// UpdateMenu
// @Tags SysMenu
// @Summary 更新菜单
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SysMenu true " "
// @Success 200 {string} string "{"success":true,"data":{},"msg":"操作成功"}"
// @Router /menu/updateMenu [put]
func UpdateMenu(c *gin.Context) {
	var menu model.SysMenu
	_ = c.ShouldBindJSON(&menu)
	if err := utils.Verify(menu); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	claims, _ := c.Get("claims")
	user := claims.(*request.CustomClaims)
	menu.UpdateTime = time.Now()
	menu.UpdateBy = user.Username

	err := service.UpdateMenu(menu)
	response.RespondOperate(err, c)
}

// QueryMenu
// @Tags SysMenu
// @Summary 查询菜单信息
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param id path int true " "
// @Success 200 {string} string "{"success":true,"data":{},"msg":"操作成功"}"
// @Router /menu/{id} [get]
func QueryMenu(c *gin.Context) {
	menuId, _ := strconv.Atoi(c.Param("id"))
	if menuId == 0 {
		response.FailWithMessage("id不能为空", c)
	}

	menu, err := service.QueryMenu(uint64(menuId))
	response.RespondData(menu, err, c)
}

// DeleteMenu
// @Tags SysMenu
// @Summary 批量删除菜单
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true " "
// @Success 200 {string} string "{"success":true,"data":{},"msg":"操作成功"}"
// @Router /menu/deleteMenu [delete]
func DeleteMenu(c *gin.Context) {
	var reqIds request.IdsReq
	_ = c.ShouldBindJSON(&reqIds)
	if err := utils.Verify(reqIds); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	err := service.DeleteMenu(reqIds.Ids)
	response.RespondOperate(err, c)
}
